package com.momo.hotel.controller;


import com.momo.hotel.api.AlipayUtil;
import com.momo.hotel.dao.pojo.TbOrder;
import com.momo.hotel.dao.pojo.TbUser;
import com.momo.hotel.dao.pojo.ext.TbShopCarExt;
import com.momo.hotel.dao.pojo.ext.TotalPrice;
import com.momo.hotel.dto.DtoOrder;
import com.momo.hotel.dto.OrderPay;
import com.momo.hotel.service.OrderService;
import com.momo.hotel.service.ShopCarService;
import com.momo.hotel.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @version 1.0.0
 * @auther Snake
 * @date 2019/7/5 9:31
 */
@Controller
public class PayController {
    private static Integer userId;
    @Resource
    OrderPay orderPay;

    @Resource
    OrderService orderService;


    //支付
    @RequestMapping(value = "roomPay",produces = "application/json;charset=utf-8")
    public void roomPay(@RequestParam String orderNo,  String totalMoney, String orderName, String orderDetail,HttpServletRequest request, HttpServletResponse response){
        orderPay.setOrderDetail(orderDetail);
        orderPay.setOrderName(orderName);
        orderPay.setOrderNo(orderNo);
        orderPay.setTotalMoney(totalMoney);
        //System.out.println("controller  !!!"+orderName);
        String s = AlipayUtil.tradePagePay(orderPay, request);



        response.setContentType("text/html;charset=utf-8");
        try {

            response.getWriter().write(s);//直接将完整的表单html输出到页面
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                response.getWriter().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //获取购物车信息
    @ResponseBody
    @RequestMapping(value = "getOrder",produces = "application/json;charset=utf-8")
    public Object getOrder(HttpServletRequest request){
        TotalPrice totalPrice = (TotalPrice) request.getSession().getAttribute("totalPrice");
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        //System.out.println("7777777777777777777777777777777777777"+user);
        userId = user.getId();
        List<TbShopCarExt> tbShopCarExt = totalPrice.getTbShopCarExt();
        String str = new String();
        for (TbShopCarExt shopCarExt : tbShopCarExt) {
            str += shopCarExt.getHotelName()+shopCarExt.getRoomType()+"房间号"+shopCarExt.getRoomID()
                    +"地址："+shopCarExt.getLocation()+"到达时间"+shopCarExt.getArrivalDate()+"离开时间"+shopCarExt.getDepartureDate();
        }

        DtoOrder dtoOrder = new DtoOrder();
        dtoOrder.setUserId(user.getId());
        dtoOrder.setOrderName("订房订单");
        dtoOrder.setTotalMoney(totalPrice.getTotalPrice());
        dtoOrder.setDescription(str);
        
        return dtoOrder;
    }

    //支付完成返回首页
    @RequestMapping(value = "returnUrl",produces = "application/json;charset=utf-8")
    public void returnUrl(HttpServletRequest request,HttpServletResponse response){
        AlipayUtil.setReturn_url(request);
        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().print("<script >alert('支付成功');location='index.html'</script>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //生成订单
    @RequestMapping(value = "notifyUrl")
    public Object notifyUrl(HttpServletRequest request, HttpServletResponse response){
        DtoOrder dtoOrder = AlipayUtil.setNotify_url(request, response);

        dtoOrder.setUserId(userId);

        boolean b = orderService.addOrder(dtoOrder);
        if (b){
            boolean b1 = orderService.delOrder(userId);
            if(b1){
                return "shop-cat";
            }else {
                return "shop-cat";
            }
        }
        System.out.println("@@@@@@@@@@@@@@@@"+b);
        return null;
    }

    //获取用户订单
    @RequestMapping(value = "getUserOrder",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object getUserOrder(HttpServletRequest request){
        TbUser user = (TbUser) request.getSession().getAttribute("user");

            List<TbOrder> order = orderService.getUserOrder(user.getId());
            return order;



    }
}
